using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ACM.cmsarea08;
using ACM.cmsarea08.Controls;
using ACM.cmsarea08.Administration;


namespace ACM.cmsarea08.Administration.Core {
	public partial class Moduli :  System.Web.UI.Page {
		private const int offSet = 20;
		protected Itan_DataGrid insModules;
		private ACM.cmsarea08.Administration.Core.DBClass dclass = new cmsarea08.Administration.Core.DBClass();
		
		private int TotalePagine {
			get {
				if(ViewState["pagine"]==null)
					return -1;
				else
					return (int)ViewState["pagine"];
			}
			set {
				ViewState["pagine"]=value;
			}
		}
		private int PaginaCorrente {
			get {
				if(ViewState["PaginaCorrente"]==null)
					return -1;
				else
					return (int)ViewState["PaginaCorrente"];
			}
			set {
				ViewState["PaginaCorrente"]=value;
			}
		}
			
		private DataSet ElencoModuli {
			get {
				if(ViewState["ElencoModuli"]==null)
					return null;
				else
					return (DataSet)ViewState["ElencoModuli"];
			}
			set {
				ViewState["ElencoModuli"]=value;
			}			
		}
		
		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

		private void InitializeComponent()
		{
			if(this.Master is ACM.cmsarea08.Administration.Core.ListerPrivate) {
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.DBActionTable="moduli";
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.HelpPath="/administration/docs/Moduli.html";
			}
			
			this.Load += new System.EventHandler(this.Page_Load);
			insModules.CambiaPagina += new Itan_DataGridEventHandler(CambiaPagina);
			insModules.SalvaOrdine  += new Itan_DataGridEventHandler(SalvaOrdine);
		}
	
		private void Page_Load(object o , EventArgs e) {
			CaricaDati();
			if(!Page.IsPostBack) {
				
			}
		}
		
		private void CaricaDati() {
			if(PaginaCorrente==-1)
				PaginaCorrente = 0;
			
			if(ElencoModuli==null)
				ElencoModuli = dclass.GetAdminModuliInst((PaginaCorrente*offSet),offSet);
			if(ElencoModuli==null) {
				insModules.Visible=false;
				return;
			}
			if(ElencoModuli.Tables[0].Columns.Contains("conteggio") && 
			   ElencoModuli.Tables[0].Rows.Count>0 ) {
				int totaleC =0;
				Int32.TryParse(ElencoModuli.Tables[0].Rows[0]["conteggio"].ToString(), out totaleC);
				TotalePagine = (totaleC/offSet) + (totaleC % offSet>1?1:0);
			}


			insModules.PageNum=TotalePagine;
			insModules.CurrentPage = PaginaCorrente;
			insModules.Visible=true;
			if(insModules.DataSource==null) {
				insModules.DataSource = ElencoModuli;
				insModules.DataBind();
			}
		}
		
		private void CambiaPagina(object o, Itan_DataGridEventArgs e) {
			PaginaCorrente=e.NewPage;
			CaricaDati();
		}
		
		private void SalvaOrdine(object o, Itan_DataGridEventArgs e) {
//			Response.Write("mi salvi l'ordine?");
			//Qui aggiorna l'ordine in funzione degli elementi ordine:
			/*
			for(int i = 0 ;i<mioDG.Items.Count;i++) {
				DataGridItem dg = (DataGridItem)mioDG.Items[i];
				Response.Write("EX QUERY; NEWORD: " + ((TextBox)dg.Cells[dg .Cells.Count-1].Controls[0]).Text + "<br>");
			}			
			*/
		}
		
		
		
	}
	
}
